<?php
declare(strict_types=1);
/*
其他入库单
*/
namespace Modules\Intelligent\Http\Controllers;

use Catch\Base\CatchController as Controller;
use Modules\Intelligent\Models\Otherstorage;
use Modules\Intelligent\Models\Otherstorageschedule;
use Illuminate\Http\Request;
use Modules\Intelligent\Http\Repositories\IntelligentReget;
use extend\services\warehouse\{warehouseService};//引入自定义类
use Illuminate\Support\Facades\DB;

class IntelligentController extends Controller
{
    public function __construct(
        protected readonly Otherstorage $model
    ){}

    /**
     * @param Request $request
     * @return mixed
     */
    public function index(Request $request): mixed
    {

        // $data = $this->model->getList();
        $data = IntelligentReget::list($request->all());
        $kingDeeService = new warehouseService();
        $datauser = $kingDeeService->uerdata();
       
        foreach(Otherstorage::$Type as $k=>$v){
           $tydata[$v['value']] = $v['label'];
        }
        foreach($data as $k=>$v){
            
            $data[$k]['creator_id'] = isset($datauser[$v['creator_id']])?$datauser[$v['creator_id']]['username']:'';
            $data[$k]['incomingtype'] = isset($tydata[$v['incomingtype']])?$tydata[$v['incomingtype']]:'';

            switch ($v['state']){
                case 0:
                    $data[$k]['description'] = '待审核'; 
                break;
                case 1:
                    $data[$k]['description'] = '已审核'; 
                break;
            }
            
            switch ($v['backstate']){

                case 1:
                    $data[$k]['writestate'] = '回写成功'; 
                break;
                case 2:
                    $data[$k]['writestate'] = '回写失败'; 
                break;
            }
        }
        
        return $data;
    }

    /**数据添加接口
     * @param Request $request
     * @return mixed
     */
    public function store(Request $request)
    {
        $data = $request->all();
        $data['data'] = $data;
        $data['creator_id'] = $this->getLoginUserId();//当前登录用户名称
        return  IntelligentReget::create($data);
    }

    /**
     * @param $id
     * @return mixed
     */
    public function show($id)
    {
        
        //return $this->model->firstBy($id);
        return  IntelligentReget::showApi($id);
    }
 
    /**
     * @param Request $request
     * @param $id
     * @return mixed
     */
    public function update($id, Request $request)
    {
        return $this->model->updateBy($id, $request->all());
    }

    /**
     * @param $id
     * @return mixed
     */
    public function destroy($id)
    {
        //return $this->model->deleteBy($id);
        return  IntelligentReget::delete($id);
    }
    
    /**条码扫描接口
     * @param $id
     * @return mixed
     */
    public function otherBarcode($code)
    {
        
        //return $this->model->deleteBy($id);
        return IntelligentReget::Barcodename($code);
    }

    /**条码扫描接口
     * @param $id
     * @return mixed
     */
    public function productname()
    {
        $material = DB::table('material')
        ->select('id','fnumber','fname','fspecification','funit_number','funit_name')
        ->get()->toArray();
        //return $this->model->deleteBy($id);

       return [
            'code'=>1000,
            'message'=>'扫描成功',
            'data'=>$material
        ];
    }

     /**
     * @param $id
     * @return mixed
     */
    public function typeData()
    {
        return [
            'code'=>1000,
            'message'=>'扫描成功',
            'data'=>Otherstorage::$Type
        ];
    }


    /*审核接口
     * @param $id
     * @return mixed
     */
    public function Auditinterface($id)
    {
        return IntelligentReget::checkPdaApi($id);
    }


     /**修改接口
     * @param $id
     * @return mixed
     */
    public function Modifyinterface($id,Request $request)
    {
        $data = $request->all();
  
        $data['modifier'] = $this->getLoginUserId();//当前登录用户名称
        return IntelligentReget::auditupdate($id,$data);
    }



}
